package state.person.events;

import messages.ByteList;
import messages.CString;
import messages.MessageUtils;
import state.group.HallCall;
import state.group.events.EventType;
import state.person.Person;
import state.person.PersonType;

public class PersonRegisterEvent extends BasePersonEvent {
    
    private Person p;
    
    public PersonRegisterEvent(byte[] data, int startIndex)
    {
        super(data, startIndex);
        int baseIndex = startIndex + personId.getLength();
        
        byte type = data[baseIndex + 1];
        
        byte startFloor = data[baseIndex + 2];
        byte startDirection = data[baseIndex + 3];
        HallCall call = new HallCall(startFloor, startDirection);
        
        byte destFloor = data[baseIndex + 4];
        int weight = MessageUtils.parseInt(data, baseIndex + 5);
        
        p = new Person(personId.toString(), PersonType.getPersonTypeForId(type), weight, call, destFloor);
        
    }
    
    public PersonRegisterEvent(Person p)
    {
        super(new CString(p.getName()));
        this.p = p;
    }
    
    public Person getPerson()
    {
        return p;
    }
    
    public void appendToByteList(ByteList list)
    {
        super.appendToByteList(list);
        
        list.addByte(p.getType().getIdentifier());
        
        list.addByte(p.getHallCall().getFloor());
        list.addByte(p.getHallCall().getDirection());
        list.addByte(p.getDestination());
        
        byte[] weight = new byte[4];
        MessageUtils.insertInt(weight, 0, p.getWeight());
        
        for (byte b : weight)
        {
            list.addByte(b);
        }
    }
    
    @Override
    public int getEventByteLength()
    {
        return 9 + personId.getLength();
    }

    @Override
    public EventType getEventType()
    {
        return EventType.PersonRegisterEvent;
    }
    
}
